'use strict';

const LoginService = require('qcloud-weapp-server-sdk').LoginService;
var co = require('co');
const User = require('../model/user');

module.exports = (req, res) => {
    const loginService = LoginService.create(req, res);

    loginService.check()
        .then(data => {
            co(function*() {
                var userInfo = data.userInfo;
                var openId = userInfo.openId;
                // 检查业务数据库是否存在该openId的用户
                var user = yield User.findOne({
                    where: {
                        openId: openId
                    }
                });
                userInfo.userId = user.id;
                res.json({
                    'code': 0,
                    'message': 'ok',
                    'data': {
                        'userInfo': userInfo,
                    },
                });
            }).catch(function(e) {
                console.log(e);
            });

        });
};